﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Xml;

namespace WeiSha.Core.Param.Method
{
	// Token: 0x02000037 RID: 55
	public class Position : IDisposable
	{
		// Token: 0x170000E1 RID: 225
		// (get) Token: 0x06000239 RID: 569 RVA: 0x0000FED4 File Offset: 0x0000E0D4
		public bool Enable
		{
			get
			{
				return LBS.Enable;
			}
		}

		// Token: 0x170000E2 RID: 226
		// (get) Token: 0x0600023A RID: 570 RVA: 0x0000FEEC File Offset: 0x0000E0EC
		public string IP
		{
			get
			{
				return this._ip;
			}
		}

		// Token: 0x170000E3 RID: 227
		// (get) Token: 0x0600023B RID: 571 RVA: 0x0000FF04 File Offset: 0x0000E104
		public decimal Latitude
		{
			get
			{
				return this._latitude;
			}
		}

		// Token: 0x170000E4 RID: 228
		// (get) Token: 0x0600023C RID: 572 RVA: 0x0000FF1C File Offset: 0x0000E11C
		public decimal Longitude
		{
			get
			{
				return this._longitude;
			}
		}

		// Token: 0x170000E5 RID: 229
		// (get) Token: 0x0600023D RID: 573 RVA: 0x0000FF34 File Offset: 0x0000E134
		public int Code
		{
			get
			{
				bool flag = this._code <= 0;
				if (flag)
				{
					this._code = LBS.GetCode(this.District);
				}
				bool flag2 = this._code <= 0;
				if (flag2)
				{
					this._code = LBS.GetCode(this.City);
				}
				bool flag3 = this._code <= 0;
				if (flag3)
				{
					this._code = LBS.GetCode(this.Province);
				}
				return this._code;
			}
		}

		// Token: 0x170000E6 RID: 230
		// (get) Token: 0x0600023E RID: 574 RVA: 0x0000FFB0 File Offset: 0x0000E1B0
		// (set) Token: 0x0600023F RID: 575 RVA: 0x00010058 File Offset: 0x0000E258
		public string Province
		{
			get
			{
				bool flag = !string.IsNullOrWhiteSpace(this._province) || string.IsNullOrWhiteSpace(this._address);
				string text;
				if (flag)
				{
					text = this._province;
				}
				else
				{
					Dictionary<int, string> dictionary = LBS.Provinces();
					foreach (KeyValuePair<int, string> keyValuePair in dictionary)
					{
						bool flag2 = this._address.IndexOf(keyValuePair.Value) > -1;
						if (flag2)
						{
							return keyValuePair.Value;
						}
					}
					text = this._province;
				}
				return text;
			}
			set
			{
				string text = (Position.IsAllChinese(this._province) ? this._province : "");
			}
		}

		// Token: 0x170000E7 RID: 231
		// (get) Token: 0x06000240 RID: 576 RVA: 0x00010078 File Offset: 0x0000E278
		// (set) Token: 0x06000241 RID: 577 RVA: 0x00010128 File Offset: 0x0000E328
		public string City
		{
			get
			{
				bool flag = !string.IsNullOrWhiteSpace(this._city) || string.IsNullOrWhiteSpace(this._address);
				string text;
				if (flag)
				{
					text = this._city;
				}
				else
				{
					Dictionary<int, string> dictionary = LBS.Cities(this.Province);
					foreach (KeyValuePair<int, string> keyValuePair in dictionary)
					{
						bool flag2 = this._address.IndexOf(keyValuePair.Value) > -1;
						if (flag2)
						{
							return keyValuePair.Value;
						}
					}
					text = this._city;
				}
				return text;
			}
			set
			{
				string text = (Position.IsAllChinese(this._city) ? this._city : "");
			}
		}

		// Token: 0x170000E8 RID: 232
		// (get) Token: 0x06000242 RID: 578 RVA: 0x00010148 File Offset: 0x0000E348
		// (set) Token: 0x06000243 RID: 579 RVA: 0x000101F8 File Offset: 0x0000E3F8
		public string District
		{
			get
			{
				bool flag = !string.IsNullOrWhiteSpace(this._district) || string.IsNullOrWhiteSpace(this._address);
				string text;
				if (flag)
				{
					text = this._district;
				}
				else
				{
					Dictionary<int, string> dictionary = LBS.Districts(this.City);
					foreach (KeyValuePair<int, string> keyValuePair in dictionary)
					{
						bool flag2 = this._address.IndexOf(keyValuePair.Value) > -1;
						if (flag2)
						{
							return keyValuePair.Value;
						}
					}
					text = this._district;
				}
				return text;
			}
			set
			{
				string text = (Position.IsAllChinese(this._district) ? this._district : "");
			}
		}

		// Token: 0x170000E9 RID: 233
		// (get) Token: 0x06000244 RID: 580 RVA: 0x00010218 File Offset: 0x0000E418
		public string Street
		{
			get
			{
				return this._street;
			}
		}

		// Token: 0x170000EA RID: 234
		// (get) Token: 0x06000245 RID: 581 RVA: 0x00010230 File Offset: 0x0000E430
		public string StreetNumber
		{
			get
			{
				return this._streetNumber;
			}
		}

		// Token: 0x170000EB RID: 235
		// (get) Token: 0x06000246 RID: 582 RVA: 0x00010248 File Offset: 0x0000E448
		public string Address
		{
			get
			{
				return this._address;
			}
		}

		// Token: 0x06000247 RID: 583 RVA: 0x00010260 File Offset: 0x0000E460
		public Position(string ip)
		{
			this._ip = ip;
			this._getPosiForIP(this._ip);
		}

		// Token: 0x06000248 RID: 584 RVA: 0x000102CC File Offset: 0x0000E4CC
		public Position(string lng, string lat)
		{
			decimal num;
			decimal.TryParse(lng, out num);
			decimal num2;
			decimal.TryParse(lat, out num2);
			this._initialize(num, num2);
		}

		// Token: 0x06000249 RID: 585 RVA: 0x00010340 File Offset: 0x0000E540
		public Position(decimal lng, decimal lat)
		{
			this._initialize(lng, lat);
		}

		// Token: 0x0600024A RID: 586 RVA: 0x0001039F File Offset: 0x0000E59F
		private void _initialize(decimal lng, decimal lat)
		{
			this._longitude = lng;
			this._latitude = lat;
			this._ip = Browser.IP;
			this._getPosiForGPS(this._longitude, this._latitude);
		}

		// Token: 0x0600024B RID: 587 RVA: 0x000103D0 File Offset: 0x0000E5D0
		public Position(string address, int type)
		{
			try
			{
				this._getPosiForAddress(address, type);
				this._getPosiForGPS(this._longitude, this._latitude);
			}
			catch
			{
			}
			this._address = address;
		}

		// Token: 0x0600024C RID: 588 RVA: 0x00010464 File Offset: 0x0000E664
		private string _getPosiForIP(string ip)
		{
			string text = "http://api.map.baidu.com/location/ip?ak={0}&ip={1}&coor=bd09ll";
			text = string.Format(text, this.ak, ip);
			WebClient webClient = new WebClient();
			webClient.BaseAddress = text;
			string text2 = "";
			try
			{
				using (Stream stream = webClient.OpenRead(text))
				{
					StreamReader streamReader = new StreamReader(stream, Encoding.Default);
					text2 = streamReader.ReadToEnd();
					streamReader.Close();
				}
			}
			catch
			{
			}
			text2 = this._unicodeToGB(text2);
			this.Province = this._getJson(text2, "province");
			this.City = this._getJson(text2, "city");
			this.District = this._getJson(text2, "district");
			this._code = this._getCode(this._province, this._city, this._district);
			this._street = this._getJson(text2, "street");
			this._streetNumber = this._getJson(text2, "street_number");
			decimal.TryParse(this._getJson(text2, "y"), out this._latitude);
			decimal.TryParse(this._getJson(text2, "x"), out this._longitude);
			this._address = this._getJson(text2, "address");
			return text2;
		}

		// Token: 0x0600024D RID: 589 RVA: 0x000105C0 File Offset: 0x0000E7C0
		private string _clearNoChinise(string text)
		{
			char[] array = text.ToCharArray();
			string text2 = "";
			for (int i = 0; i < array.Length; i++)
			{
				bool flag = array[i] >= '一' && array[i] <= '龻';
				if (flag)
				{
					text2 += array[i].ToString();
				}
			}
			return text2;
		}

		// Token: 0x0600024E RID: 590 RVA: 0x0001062C File Offset: 0x0000E82C
		private string _unicodeToGB(string text)
		{
			MatchCollection matchCollection = Regex.Matches(text, "\\\\u([\\w]{4})");
			bool flag = matchCollection != null && matchCollection.Count > 0;
			if (flag)
			{
				foreach (object obj in matchCollection)
				{
					Match match = (Match)obj;
					string value = match.Value;
					string text2 = value.Substring(2);
					byte[] array = new byte[2];
					int num = Convert.ToInt32(text2.Substring(0, 2), 16);
					int num2 = Convert.ToInt32(text2.Substring(2), 16);
					array[0] = (byte)num2;
					array[1] = (byte)num;
					text = text.Replace(value, Encoding.Unicode.GetString(array));
				}
			}
			return text;
		}

		// Token: 0x0600024F RID: 591 RVA: 0x00010710 File Offset: 0x0000E910
		private string _getJson(string result, string key)
		{
			string text = "\"{0}\":\"(?<value>[^\"]+)\"";
			text = string.Format(text, key);
			Regex regex = new Regex(text, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);
			MatchCollection matchCollection = regex.Matches(result);
			string text2 = "";
			foreach (object obj in matchCollection)
			{
				Match match = (Match)obj;
				text2 = match.Groups["value"].Value.Trim();
			}
			return text2;
		}

		// Token: 0x06000250 RID: 592 RVA: 0x000107B0 File Offset: 0x0000E9B0
		private int _getCode(string province, string city, string district)
		{
			int num = LBS.GetCode(district);
			bool flag = num <= 0;
			if (flag)
			{
				num = LBS.GetCode(city);
			}
			bool flag2 = num <= 0;
			if (flag2)
			{
				num = LBS.GetCode(province);
			}
			return num;
		}

		// Token: 0x06000251 RID: 593 RVA: 0x000107F0 File Offset: 0x0000E9F0
		private void _getPosiForGPS(decimal lng, decimal lat)
		{
			bool flag = lng <= 0m || lat <= 0m;
			if (!flag)
			{
				string text = "http://api.map.baidu.com/geocoder/v2/?ak={0}&location={1},{2}&output=xml&pois=0";
				text = string.Format(text, this.ak, lat, lng);
				WebClient webClient = new WebClient();
				webClient.BaseAddress = text;
				string text2 = "";
				try
				{
					using (Stream stream = webClient.OpenRead(text))
					{
						StreamReader streamReader = new StreamReader(stream, Encoding.UTF8);
						text2 = streamReader.ReadToEnd();
						streamReader.Close();
					}
				}
				catch
				{
				}
				try
				{
					XmlDocument xmlDocument = new XmlDocument();
					xmlDocument.LoadXml(text2);
					XmlNode xmlNode = xmlDocument.SelectSingleNode("GeocoderSearchResponse/status");
					bool flag2 = xmlNode.InnerText == "0";
					if (flag2)
					{
						XmlNode xmlNode2 = xmlDocument.SelectSingleNode("GeocoderSearchResponse/result/formatted_address");
						this._address = xmlNode2.InnerText;
						XmlNode xmlNode3 = xmlDocument.SelectSingleNode("GeocoderSearchResponse/result/addressComponent");
						this.Province = xmlNode3.SelectSingleNode("province").InnerText;
						this.City = xmlNode3.SelectSingleNode("city").InnerText;
						this.District = xmlNode3.SelectSingleNode("district").InnerText;
						this._code = this._getCode(this._province, this._city, this._district);
						this._street = xmlNode3.SelectSingleNode("street").InnerText;
						this._streetNumber = xmlNode3.SelectSingleNode("street_number").InnerText;
					}
				}
				catch
				{
				}
			}
		}

		// Token: 0x06000252 RID: 594 RVA: 0x000109C0 File Offset: 0x0000EBC0
		private void _getPosiForAddress(string address, int type)
		{
			string text = ((type == 1) ? "xml" : "json");
			address = HttpUtility.UrlEncode(address);
			string text2 = "http://api.map.baidu.com/geocoder/v2/?ak={0}&output={1}&address={2}";
			text2 = string.Format(text2, this.ak, text, address);
			WebClient webClient = new WebClient();
			webClient.BaseAddress = text2;
			string text3 = "";
			try
			{
				using (Stream stream = webClient.OpenRead(text2))
				{
					StreamReader streamReader = new StreamReader(stream, Encoding.UTF8);
					text3 = streamReader.ReadToEnd();
					streamReader.Close();
				}
			}
			catch (Exception ex)
			{
				throw ex;
			}
			try
			{
				XmlDocument xmlDocument = new XmlDocument();
				xmlDocument.LoadXml(text3);
				XmlNode xmlNode = xmlDocument.SelectSingleNode("GeocoderSearchResponse/status");
				bool flag = xmlNode.InnerText == "0";
				if (flag)
				{
					XmlNode xmlNode2 = xmlDocument.SelectSingleNode("GeocoderSearchResponse/result/location");
					decimal.TryParse(xmlNode2.SelectSingleNode("lng").InnerText, out this._longitude);
					decimal.TryParse(xmlNode2.SelectSingleNode("lat").InnerText, out this._latitude);
				}
			}
			catch (Exception ex2)
			{
				throw ex2;
			}
		}

		// Token: 0x06000253 RID: 595 RVA: 0x00010B04 File Offset: 0x0000ED04
		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		// Token: 0x06000254 RID: 596 RVA: 0x00010B18 File Offset: 0x0000ED18
		protected virtual void Dispose(bool disposing)
		{
			bool flag = !this.disposed;
			if (flag)
			{
				if (disposing)
				{
				}
				this.disposed = true;
			}
		}

		// Token: 0x06000255 RID: 597 RVA: 0x00010B44 File Offset: 0x0000ED44
		~Position()
		{
			this.Dispose(false);
		}

		// Token: 0x06000256 RID: 598 RVA: 0x00010B78 File Offset: 0x0000ED78
		private static bool IsAllChinese(string input)
		{
			bool flag = string.IsNullOrWhiteSpace(input);
			return !flag && Regex.IsMatch(input, "^[\\u4e00-\\u9fa5]+$");
		}

		// Token: 0x040000A4 RID: 164
		private string ak = LBS.Get["BaiduAK"].String;

		// Token: 0x040000A5 RID: 165
		private string _ip;

		// Token: 0x040000A6 RID: 166
		private decimal _latitude;

		// Token: 0x040000A7 RID: 167
		private decimal _longitude;

		// Token: 0x040000A8 RID: 168
		private int _code;

		// Token: 0x040000A9 RID: 169
		private string _province = string.Empty;

		// Token: 0x040000AA RID: 170
		private string _city = string.Empty;

		// Token: 0x040000AB RID: 171
		private string _district = string.Empty;

		// Token: 0x040000AC RID: 172
		private string _street;

		// Token: 0x040000AD RID: 173
		private string _streetNumber;

		// Token: 0x040000AE RID: 174
		private string _address;

		// Token: 0x040000AF RID: 175
		private bool disposed = false;
	}
}
